<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class DocComment {
    const ANNOTATION_PATTERN = '/^@(?P<name>\w[\w\d]*)(?P<value>.*)$/';
    
    /**
     * @var Annotation[]
     */
    private $annotations = array();
    
    /**
     * @var string
     */
    private $description;
    
    /**
     * @var string
     */
    private $brief;
    
    /**
     * @param string $docComment
     * @return DocComment 
     */
    public static function parseDocComment($docComment) {
        $doc = new DocComment();
        $description = array();
        
        foreach (preg_split("/\r?\n/", $docComment) as $line) {
            $line = rtrim(ltrim($line, ' */'));
            
            if (!$line) {
                continue;
            }
            
            if (preg_match(self::ANNOTATION_PATTERN, $line, $match)) {
                $name = $match['name'];
                $value = trim($match['value']);
                
                $annotation = new Annotation();
                $annotation->setName($name);
                $annotation->setValue($value);
                
                $doc->putAnnotation($annotation);
            }
            else {
                $description[] = rtrim($line);
            }
        }
        
        $brief = $doc->getAnnotation('brief');
        if ($brief !== null) {
            $doc->setBrief($brief->getValue());
        }
        else {
            $doc->setBrief(array_shift($description));
        }
        $doc->setDescription(implode("\n", $description));
        
        return $doc;
    }
    
    /**
     * @param string $name
     * @return Annotation[]
     */
    public function getAnnotationsLike($name) {
        return array_get($this->annotations, $name, array());
    }
    
    /**
     * @param string $name 
     * @return Annotation or null if there is no such annotation
     */
    public function getAnnotation($name) {
        $annotations = $this->getAnnotationsLike($name);
        if (!empty($annotations)) {
            return $annotations[0];
        }
        else {
            return null;
        }
    }
    
    /**
     * @param Annotation $annotation
     */
    public function putAnnotation(Annotation $annotation) {
        $this->annotations[$annotation->getName()][] = $annotation;
    }

    /**
     * @return string
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * @param string $description 
     */
    public function setDescription($description) {
        $this->description = $description;
    }

    /**
     *
     * @return string A brief description
     */
    public function getBrief() {
        return $this->brief;
    }

    /**
     * @param string $brief A brief description
     */
    public function setBrief($brief) {
        $this->brief = $brief;
    }
}
